<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    MySQL 主从复制 |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/assets/img/favicon.ico" />
  
  <link rel="stylesheet" href="/dist/main.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  <link rel="stylesheet" href="/css/custom.css">
  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?e6614c2d092d0bce54a2543d8cf949fa";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>


<link rel="alternate" href="/atom.xml" title="null" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-MySQL-主从复制"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  MySQL 主从复制
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2019/01/03/MySQL-主从复制/" class="article-date">
  <time datetime="2019-01-03T13:16:39.000Z" itemprop="datePublished">2019-01-03</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/技术/">技术</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.7k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">6 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>主从复制也称为主从同步，将一台主机的数据复制到另一台或多台主机上，通过中继日志 relay log 来实现复制功能，是构建数据库高可用集群架构的基础。</p>
<p>复制过程中一台服务器充当主库 master，其余一个或多个服务器充当从库 slave。</p>
<a id="more"></a>
<h2 id="主从复制功能"><a href="#主从复制功能" class="headerlink" title="主从复制功能"></a>主从复制功能</h2><p>主从复制有如下功能：</p>
<ul>
<li>实时备灾，从库随时接管有故障的主库</li>
<li>从库分担主库的读压力，读写分离</li>
<li>利用从库做备份，减少对业务的影响</li>
<li>利用从库完成 MySQL 平滑的版本升级</li>
</ul>
<h2 id="主从复制原理"><a href="#主从复制原理" class="headerlink" title="主从复制原理"></a>主从复制原理</h2><p>主库把 SQL 请求记录到 binlog 日志中，从库的 IO thread 向主库请求 binlog 日志，主库通过 IO dump thread 传送 binlog，从库将得到的 binlog 写入中继日志 relay log，然后 SQL thread 通过中继日志重做 SQL 语句。</p>
<h2 id="复制中的重要参数"><a href="#复制中的重要参数" class="headerlink" title="复制中的重要参数"></a>复制中的重要参数</h2><ul>
<li>log-bin 搭建主从复制必须开启 binlog</li>
<li>server-id 同一组主从架构中的唯一标识</li>
<li>server-uuid 由 MySQL 自动生成，每台机器不一样，在数据目录下的 auto.cnf 中</li>
<li>read-only 设置从库为只读状态（对 super 账号没有效果）</li>
<li>super-read-only 设置 super 账号是否开启只读状态</li>
<li>binlog-format 二进制日志格式，必须使用 row 模式（默认 row）</li>
<li>log_slave_updates 将源于主库的数据变更信息记录到从库 binlog 中</li>
<li>todo</li>
</ul>
<h2 id="异步复制"><a href="#异步复制" class="headerlink" title="异步复制"></a>异步复制</h2><p>异步复制为 MySQL 默认的复制方式，主库写入 binlog 后即可成功返回给客户端，无须等待 binlog 传递给从库。</p>
<blockquote>
<p>异步复制的缺点是主库一旦发生宕机在高可用架构下做主备切换可能导致数据丢失。</p>
</blockquote>
<p>以下为基于 binlog 和 position 的方式（非 GTID）搭建一主一从架构的步骤：</p>
<ol>
<li>修改主库和从库的 server-id，确保两者不一致。</li>
<li>主库开启 binlog 功能（配置 log_bin）。</li>
<li>从库最好开启 log_slave_updates 参数，让从库写 binlog，方便扩展。</li>
<li>确保 binlog 格式为 row（默认为 row）。</li>
<li>在主库中创建主从复制账号。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; create user &apos;bak&apos;@&apos;%&apos; identified by &apos;bak123&apos;;</span><br><span class="line">mysql&gt; grant replication slave on *.* to &apos;bak&apos;@&apos;%&apos;;</span><br><span class="line">mysql&gt; flush privileges;</span><br></pre></td></tr></table></figure>
<ol start="6">
<li>通过 mysqldump 导出主库数据。</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> /usr/local/mysql/bin/mysqldump --single-transaction -uroot -pxxx --master-data=2 -A &gt; all.sql</span><br></pre></td></tr></table></figure>
<blockquote>
<p>–master-data=2 会在导出文件中记录当前使用的 binlog 文件及 position 号，并注释掉 change master 语句，change master 语句后续会手动执行。</p>
</blockquote>
<ol start="7">
<li>从库导入主库数据。</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> /usr/local/mysql/bin/mysql -uroot -pxxx &lt; all.sql</span><br></pre></td></tr></table></figure>
<ol start="8">
<li>在从库执行配置主从的命令。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; change master to master_host=&apos;192.168.10.10&apos;,master_user=&apos;bak&apos;,master_password=&apos;bak123&apos;,master_port=3306,master_log_file=&apos;master-bin.000012&apos;,master_log_pos=1230;</span><br></pre></td></tr></table></figure>
<ol start="9">
<li>执行开始主从复制的命令并查看复制状态，从库 IO thread 和 SQL thread 均为 Yes 表示已经开始复制工作。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; start slave;</span><br><span class="line">mysql&gt; show slave status;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>另 stop slave 用于关闭主从复制，reset slave all 用于清空从库复制配置。</p>
</blockquote>
<h2 id="半同步复制"><a href="#半同步复制" class="headerlink" title="半同步复制"></a>半同步复制</h2><p>MySQL 5.5 之后引入半同步复制功能，开启后主库需确保从库接收完 binlog 并写入到 relay log 中才会通知等待线程操作完毕。如等待超时则关闭半同步复制，自动转为异步复制，直到至少有一台从库通知主库已经接收到 binlog 为止。</p>
<h3 id="半同步复制相关参数"><a href="#半同步复制相关参数" class="headerlink" title="半同步复制相关参数"></a>半同步复制相关参数</h3><ul>
<li>rpl_semi_sync_master_timeout</li>
</ul>
<p>关闭半同步复制的超时时间，单位毫秒，默认 10000，可以调整得很大来禁止切换异步复制，以提高复制安全性。</p>
<ul>
<li>rpl_semi_sync_master_wait_point</li>
</ul>
<p>用于控制半同步模式下事务的提交方式，包含 AFTER_COMMIT 和 AFTER_SYNC 两个值，AFTER_COMMIT 即将 binlog 传送给从库同时主库提交事务，AFTER_SYNC 即收到从库反馈后主库再提交事务。5.7 默认为 AFTER_SYNC，能够确保主库上提交的事务都已经同步给从库。</p>
<ul>
<li>rpl_semi_sync_master_wait_for_slave_count</li>
</ul>
<p>控制主库需等待接收写入成功反馈的从库个数，当从库故障时可以通过该参数剔除故障从库。</p>
<h3 id="半同步复制搭建过程"><a href="#半同步复制搭建过程" class="headerlink" title="半同步复制搭建过程"></a>半同步复制搭建过程</h3><p>半同步复制的搭建基于异步复制，搭建完异步复制后只需安装半同步复制插件即可，以下为详细步骤：</p>
<ol>
<li>主库安装半同步复制插件并开启半同步复制功能。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; install plugin rpl_semi_sync_master soname &apos;semisync_master.so&apos;;</span><br><span class="line">mysql&gt; set global rpl_semi_sync_master_enabled=on;</span><br><span class="line">mysql&gt; show variables like &apos;%semi%&apos;;</span><br><span class="line">mysql&gt; show plugins;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>从库安装半同步复制插件并开启半同步复制功能。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; install plugin rpl_semi_sync_slave soname &apos;semisync_slave.so&apos;;</span><br><span class="line">mysql&gt; set global rpl_semi_sync_slave_enabled=on;</span><br><span class="line">mysql&gt; show variables like &apos;%semi%&apos;;</span><br><span class="line">mysql&gt; show plugins;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>为了开机启动半同步复制功能，可以在主库和从库的配置文件 my.cnf 中分别配置 rpl_semi_sync_master_enabled=on 和 rpl_semi_sync_slave_enabled=on。</p>
</blockquote>
<ol start="3">
<li>重启从库 IO 线程并检查主库和从库的。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">## 从库重启 IO 线程</span><br><span class="line">mysql&gt; stop slave io_thread;</span><br><span class="line">mysql&gt; start slave io_thread;</span><br><span class="line"></span><br><span class="line">## 检查主库半同步复制状态，注意 Rpl_semi_sync_master_status 和 Rpl_semi_sync_master_clients</span><br><span class="line">mysql&gt; show global status like &apos;%semi%&apos;;</span><br><span class="line"></span><br><span class="line">## 检查从库半同步复制状态，注意 Rpl_semi_sync_slave_status</span><br><span class="line">mysql&gt; show global status like &apos;%semi%&apos;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>可以通过 <code>show global status like &#39;%semi%&#39;</code> 来查看主库接收从库事务回复的成功失败次数，分别为 Rpl_semi_sync_master_yes_tx 和 Rpl_semi_sync_master_no_tx。</p>
</blockquote>
<p>至此 MySQL 半同步复制搭建成功。</p>
<h3 id="半同步复制和异步复制切换"><a href="#半同步复制和异步复制切换" class="headerlink" title="半同步复制和异步复制切换"></a>半同步复制和异步复制切换</h3><p>半同步复制状态下，如等待超时则关闭半同步复制，自动转为异步复制，当主库接收到来自从库的 binlog 反馈后会重新转为半同步复制。</p>
<p>比如在半同步复制状态下关闭从库，在主库中写入数据，写入数据的 SQL 会等待至半同步超时然后返回，此时通过 <code>show global status like &#39;%semi%&#39;</code> 可以看到主库已经关闭了半同步复制。</p>
<p>重新启动从库，如果从库配置了 <code>rpl_semi_sync_slave_enabled=on</code> ，则启动后主库从库自动转为半同步复制模式，否则会依然处于异步复制模式，需手动开启从库半同步复制然后重启从库 IO 线程才会转为半同步复制。</p>
<h2 id="并行复制"><a href="#并行复制" class="headerlink" title="并行复制"></a>并行复制</h2><p>MySQL 5.6 开始支持基于库级别的并行复制，及 slave_parallel_type=database。MySQL 5.7 中实现了基于组提交的并行复制，即从库可以通过多个 workers 线程并发执行 relay log 中的事务。</p>
<p>要开启基于组提交的并行复制，需要设置 slave_parallel_workers &gt; 0，一般设置为 8 或 16，并将 slave_parallel_type 设置为 LOGICAL_CLOCK。</p>
<p>并行复制可以有效降低主从延迟，其他可以降低主从延迟的方法有采用 Percona 的 percona-xtradb-cluster (PXC) 架构多节点写入，选择合适的分库分表策略，避免无效 IO，使用高速磁盘，适当调整 buffer pool 大小等。</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://blog.ulyssesss.com/2019/01/03/MySQL-主从复制/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/MySQL/">MySQL</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2019/06/11/设计模式之策略模式/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            设计模式之策略模式
          
        </div>
      </a>
    
    
      <a href="/2019/01/02/MySQL-备份恢复/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">MySQL 备份恢复</div>
      </a>
    
  </nav>

   
 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2016-2021
        <i class="ri-heart-fill heart_icon"></i> 大雪碧
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/assets/img/head.svg" alt=""></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>谢谢老板</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/assets/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/assets/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>
<!-- Tocbot -->

<script src="/js/tocbot.min.js"></script>
<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>
<script src="/dist/main.js"></script>
<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->

<!-- busuanzi  -->

<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->

<link rel="stylesheet" href="/css/clipboard.css">
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>